/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-06     LENOVO       the first version
 */

#include <rtdbg.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <rtdef.h>
#include <stdio.h>
#include <string.h>

#include <PetAction.h>
#include <SU_03T_USART.h>// 包含头文件
#include "OLED_DEV.h"
#include "FACE_DATA.h"


uint16_t SpeedDelay = 200;
rt_thread_t action_th;

void Action_advance_entry(void)//前进循环
{
    uint8_t cnt = 3;
    while (cnt != 0)
    {
        pwm1_set_angle(45);
        pwm4_set_angle(45);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x10) break;
        pwm2_set_angle(135);
        pwm3_set_angle(135);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x10) break;
        pwm1_set_angle(90);
        pwm4_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x10) break;
        pwm2_set_angle(90);
        pwm3_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x10) break;

        pwm2_set_angle(45);
        pwm3_set_angle(45);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x10) break;
        pwm1_set_angle(135);
        pwm4_set_angle(135);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x10) break;
        pwm2_set_angle(90);
        pwm3_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x10) break;
        pwm1_set_angle(90);
        pwm4_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x10) break;

        cnt--;
        rt_thread_mdelay(10); // 避免CPU占用过高
    }
}

void Action_back_entry(void)//后退循环
{
    uint8_t cnt = 3;
    while (cnt != 0)
    {
        pwm1_set_angle(135);
        pwm4_set_angle(135);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x20)break;
        pwm2_set_angle(45);
        pwm3_set_angle(45);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x20)break;
        pwm1_set_angle(90);
        pwm4_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x20)break;
        pwm2_set_angle(90);
        pwm3_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x20)break;

        pwm2_set_angle(135);
        pwm3_set_angle(135);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x20)break;
        pwm1_set_angle(45);
        pwm4_set_angle(45);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x20)break;
        pwm2_set_angle(90);
        pwm3_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x20)break;
        pwm1_set_angle(90);
        pwm4_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x20)break;

        cnt--;
        rt_thread_mdelay(10); // 避免CPU占用过高
    }
}

void Action_left_entry(void)//左转循环
{
    uint8_t cnt = 3;
    while (cnt != 0)
    {

        pwm1_set_angle(45);
        pwm4_set_angle(135);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x30)break;
        pwm2_set_angle(45);
        pwm3_set_angle(135);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x30)break;
        pwm1_set_angle(90);
        pwm4_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x30)break;
        pwm2_set_angle(90);
        pwm3_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x30)break;

        cnt--;
        rt_thread_mdelay(10); // 避免CPU占用过高
    }
}

void Action_right_entry(void)//右转循环
{
    uint8_t cnt = 3;
    while (cnt != 0)
    {

        pwm2_set_angle(45);
        pwm3_set_angle(135);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x40)break;
        pwm1_set_angle(45);
        pwm4_set_angle(135);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x40)break;
        pwm2_set_angle(90);
        pwm3_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x40)break;
        pwm1_set_angle(90);
        pwm4_set_angle(90);
        rt_thread_mdelay(SpeedDelay);
        if(received_data != 0x40)break;

        cnt--;
        rt_thread_mdelay(10); // 避免CPU占用过高
    }
}

void Action_getdown_entry(void)//坐下
{
    uint8_t cnt = 1;
    while (cnt != 0)
    {

        pwm1_set_angle(90);
        pwm2_set_angle(90);
        rt_thread_mdelay(80);
        pwm3_set_angle(20);
        pwm4_set_angle(20);

        cnt--;
        rt_thread_mdelay(10); // 避免CPU占用过高
    }
}

void Action_standup_entry(void)//起立
{
    uint8_t cnt = 1;
    while (cnt != 0)
    {

        pwm1_set_angle(90);
        pwm2_set_angle(90);
        rt_thread_mdelay(80);
        pwm3_set_angle(90);
        pwm4_set_angle(90);

        cnt--;
        rt_thread_mdelay(10); // 避免CPU占用过高
    }
}

void Action_shakehands_entry(void)//握手
{
    uint8_t cnt = 3;

    pwm1_set_angle(90);
    pwm2_set_angle(90);
    rt_thread_mdelay(80);
    pwm3_set_angle(20);
    pwm4_set_angle(20);
    rt_thread_mdelay(500);//先坐下

    while (cnt != 0)
    {


        pwm2_set_angle(25);
        rt_thread_mdelay(300);
        if(received_data != 0x70)break;
        pwm2_set_angle(45);
        rt_thread_mdelay(300);
        if(received_data != 0x70)break;
        pwm2_set_angle(25);
        rt_thread_mdelay(300);
        if(received_data != 0x70)break;
        pwm2_set_angle(45);
        rt_thread_mdelay(300);
        if(received_data != 0x70)break;
        pwm2_set_angle(25);
        rt_thread_mdelay(300);
        if(received_data != 0x70)break;
        pwm2_set_angle(45);
        rt_thread_mdelay(300);
        if(received_data != 0x70)break;//左前舵机转动

        cnt--;
    }

    pwm1_set_angle(90);
    pwm2_set_angle(90);
    rt_thread_mdelay(80);
    pwm3_set_angle(90);
    pwm4_set_angle(90);//最后起立
}

void Action_waggingtail_entry(void)//摇尾巴
{
//    while (1)
//    {
//        if (received_data == 0x80)
//        {
//            PAnumbers = Chongfunumber;
//            while ((PAnumbers || Sustainedmove) && received_data == 0x80)
//            {
////                pwm1_set_angle(45);
////                pwm2_set_angle(45);
////                rt_thread_mdelay(SpeedDelay);
////
////                pwm3_set_angle(45);
////                pwm4_set_angle(45);
////                rt_thread_mdelay(SpeedDelay);
////
////                pwm1_set_angle(90);
////                pwm2_set_angle(90);
////                rt_thread_mdelay(SpeedDelay);
////
////                pwm3_set_angle(90);
////                pwm4_set_angle(90);
////                rt_thread_mdelay(SpeedDelay);
//            }
//        }
//        rt_thread_mdelay(10); // 避免CPU占用过高
//    }
}

void action_thread_enter(void){

    while(1){
        if (received_data == 0x10) {
            OLED_ShowImage(Face_mania);
            Action_advance_entry();
        }else if (received_data == 0x20) {
            OLED_ShowImage(Face_mania);
            Action_back_entry();
        }else if (received_data == 0x30) {
            OLED_ShowImage(Face_happy);
            Action_left_entry();
        }else if (received_data == 0x40) {
            OLED_ShowImage(Face_happy);
            Action_right_entry();
        }else if (received_data == 0x50) {
            OLED_ShowImage(Face_stare);
            Action_getdown_entry();
        }else if (received_data == 0x60) {
            OLED_ShowImage(Face_stare);
            Action_standup_entry();
        }else if (received_data == 0x70) {
            OLED_ShowImage(Face_very_happy);
            Action_shakehands_entry();
        }else if (received_data == 0x80) {
            OLED_ShowImage(Face_hello);
            Action_waggingtail_entry();
        }


        received_data = 0x00;
        rt_thread_mdelay(500);
    }
}
void action_thread(void){
    action_th = rt_thread_create("action_th", action_thread_enter, NULL, 1024, 10, 5);

    rt_thread_startup(action_th);

    rt_kprintf("action_thread run success! \n");

}














